import React, {Component} from 'react';
import {base, high} from 'nc-lightapp-front';
import getCompItem from "./getCompItem";
import {formatDate} from "../../../../../../../hrpub/common/utils/utils";

const {NCCheckbox} = base;

function toggleAllCheck(value) {
    let flagState = {};
    const {endflag_flag, enddate_flag} = this.state;
    if (!endflag_flag && !enddate_flag) {
        this.setState({
            endflag_flag: true,
            endflag: true,
            enddate_flag: true,
            enddate: formatDate(new Date())
        })
    }
    this.fieldKeys.forEach(item => flagState[item + '_flag'] = value);
    this.setState(flagState);
}

function getAllCheckStatus() {
    const checkedField = this.fieldKeys.filter(item => this.state[item + '_flag']);
    return checkedField.length === this.fieldKeys.length;
}

function getIndeterminateStatus() {
    const checkedField = this.fieldKeys.filter(item => this.state[item + '_flag']);
    return checkedField.length < this.fieldKeys.length && checkedField.length > 0;
}

function getItem(item) {
    return getCompItem.call(this, item)
}

export default function getComponent() {
    return <div className='batch-edit-comp'>
        <div>
            <NCCheckbox
                indeterminate={getIndeterminateStatus.call(this)}
                onChange={toggleAllCheck.bind(this)}
                checked={getAllCheckStatus.call(this)}
            >{this.props.json['hi6007-000107']}</NCCheckbox>{/* 国际化处理： 全部启用*/}
        </div>
        {this.config.map(item => getItem.call(this, item))}
    </div>
}
